import { editPasswordFn } from "@/api/login";
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";

export function useEditPassword() {
  // 修改密码
  const dialogEditPassword = ref(false);
  //   form表单数据
  const ruleFormEdit = reactive({
    oldpassword: "",
    password: "",
    repassword: "",
  });
  const ruleFormRefEdit = ref(null);
  //   表单验证规则
  const rulesEdit = reactive({
    oldpassword: [
      { required: true, message: "请输入原始密码", trigger: "blur" },
    ],
    password: [{ required: true, message: "请输入新密码", trigger: "blur" }],
    repassword: [
      { required: true, message: "请输入确认密码", trigger: "blur" },
    ],
  });
//   修改密码api

  const editPasswordHandle = () => {
    ruleFormRefEdit.value.validate(async (isValid) => {
      if (!isValid) {
        return;
      }
      // 验证通过，调用修改密码接口
      const res = await editPasswordFn(ruleFormEdit);
      if (res.msg != "ok") {
        return ElMessage(res.msg);
      }
      // 修改密码成功
      ElMessage({
        message: "密码修改成功",
        type: "success",
      });
      // 关闭对话框
      dialogEditPassword.value = false;
    });
  };
  return {
    dialogEditPassword,
    ruleFormEdit,
    rulesEdit,
    ruleFormRefEdit,
    editPasswordHandle
  }
}
